package com.example.testproject.ext

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.testproject.R


/**
 *
 * @ClassName: ViewExt
 * @Author: android 超级兵
 * @CreateDate: 10/11/21$ 5:40 PM$
 * TODO View 的一些扩展
 */

fun <T> ImageView.loadUrl(url: T, width: Int = 900, height: Int = 1600, angle: Int = 1) {
    //设置图片圆角
    val roundedCorners = RoundedCorners(angle)
    //通过RequestOptions扩展功能
    val options: RequestOptions =
        RequestOptions.bitmapTransform(roundedCorners)
            .placeholder(R.mipmap.no_data) //设置占位图
            .error(R.mipmap.no_data) //设置错误图片
            .fallback(R.mipmap.no_data)
            .override(width, height) //采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗

    Glide.with(context)
        .load(url)
        .apply(options) //fixme 注意加载圆角的时候，注释掉下面的方法
        .into(this)
}

/*
 * 作者:android 超级兵
 * 创建时间: 10/28/21 9:44 AM
 * TODO  EditText输入扩展
 */
fun EditText.addOnTextChangedClick(onTextChanged: (String) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            onTextChanged(s.toString())
        }

        override fun afterTextChanged(s: Editable?) {
        }
    })
}